package com.rz.htk.destination.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.core.exception.GlobalException;
import com.rz.htk.destination.entity.Destination;
import com.rz.htk.destination.mapper.DestinationMapper;
import com.rz.htk.destination.service.IDestinationService;
import com.rz.htk.serviceprovider.entity.ServiceProvider;
import com.rz.htk.serviceprovider.service.IServiceProviderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.krb5.internal.crypto.Des;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName DestinationServiceImpl
 * @Description 目的港service实现类
 * @Author maweiyue
 * @Date 2019/9/6 13:37
 */
@Service
public class DestinationServiceImpl extends ServiceImpl<DestinationMapper, Destination> implements IDestinationService {

    @Resource
    private IServiceProviderService providerService;

    /**
     * 根据目的地国家获取目的港列表
     * @param country 国家2字节代码
     * @return
     */
    public List<Destination> listByCountry(String country){
        QueryWrapper<Destination> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("country",country);
        return list(queryWrapper);
    }

    /**
     * 根据目的地国家获取目的港列表
     * @param country 国家2字节代码
     * @param zip 目的港邮编
     * @return
     */
    public Destination getDestination(String country, String zip){
        QueryWrapper<Destination> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("country",country);
        queryWrapper.likeRight("zip",zip);
        List<Destination> list =list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public IPage<Destination> getPage(Destination destination) {
        QueryWrapper<Destination> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(destination.getName())) {
            wrapper.like("prd_name", destination.getName());
        }
        Page<Destination> page = new Page<>(destination.getPageNo(), destination.getPageSize());
        IPage<Destination> desktopIPage = page(page, wrapper);
        buildResult(desktopIPage.getRecords());
        return desktopIPage;
    }

    private void buildResult(List<Destination> records) {
        List<Integer> spIds = new ArrayList<>();
        for (Destination des : records) {
            spIds.add(des.getServiceProviderId());
        }
        Collection<ServiceProvider> serviceProviderList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(spIds)) {
            serviceProviderList = providerService.listByIds(spIds);
        }
        for (Destination des : records) {
            for (ServiceProvider sp : serviceProviderList) {
                if (sp.getId().equals(des.getServiceProviderId())) {
                    des.setServiceProvider(sp.getName());
                }
            }
        }
    }

    @Override
    @Transactional
    public boolean saveDestination(Destination destination){
        boolean isOk = false;
        if (destination.getId() == null) {
            if (CollectionUtils.isNotEmpty(listDestinationByName(destination.getName()))) {
                throw new GlobalException("目的港名称【" + destination.getName() + "】已存在！");
            }
        } else {
            List<Destination> oldDestinationList = listDestinationByName(destination.getName());
            if (CollectionUtils.isNotEmpty(oldDestinationList) && !destination.getId().equals(oldDestinationList.get(0).getId())) {
                throw new GlobalException("目的港名称【" + destination.getName() + "】已存在！");
            }
        }
        isOk = this.saveOrUpdate(destination);
        if (!isOk) {
            throw new GlobalException("目的港信息保存失败！");
        }
        return isOk;
    }

    @Override
    @Transactional
    public boolean batchDelete(Integer id){
        Destination destination = this.getById(id);
        if (destination != null) {
            boolean isOk = this.removeById(id);
            if (!isOk) {
                throw new GlobalException("目的港信息【" + destination.getName() + "】删除失败！");
            }
        } else {
            throw new GlobalException("目的港信息不存在，删除失败！");
        }
        return true;
    }

    public List<Destination> listDestinationByName(String name){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", name);
        return list(queryWrapper);
    }

}
